package com.yeraphael.utils;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JWTUtils {

    private static String signKey = "IPY31AnRL7Mhf2dZuCw/BQDPWAFZ3uXjNiA/2rMJ24g=";
    private static Long expire = 43200000L;
    
    public static String generateJWT( Map<String, Object> claims) {
//        Map<String, Object> claims = new HashMap();
//        claims.put("id", "1");
//        claims.put("username", "yeraphael");
//        SecretKey key = Keys.secretKeyFor(SignatureAlgorithm.HS256); // 自动生成符合规范的 key
//        System.out.println(key.getEncoded().length * 8 + " bits"); // 打印长度，应该是 256 bits
//        String base64Key = Base64.getEncoder().encodeToString(key.getEncoded());
//        System.out.println("Base64 Key: " + base64Key);

        String jwt = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, signKey)
                .addClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis() + expire))
                .compact();
//        System.out.println(jwt);
        return jwt;
    }


    public static Claims parseJWT(String jwt) {
        Claims claims = Jwts.parser().setSigningKey(signKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }
}
